package com.kxmall.web.controller.categorize;

import java.util.List;
import java.util.Arrays;

import com.kxmall.categorize.domain.bo.HsWasteCategorizeBo;
import com.kxmall.categorize.domain.vo.HsWasteCategorizeVo;
import com.kxmall.categorize.service.IHsWasteCategorizeService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.kxmall.common.annotation.RepeatSubmit;
import com.kxmall.common.annotation.Log;
import com.kxmall.common.core.controller.BaseController;
import com.kxmall.common.core.domain.PageQuery;
import com.kxmall.common.core.domain.R;
import com.kxmall.common.core.validate.AddGroup;
import com.kxmall.common.core.validate.EditGroup;
import com.kxmall.common.enums.BusinessType;
import com.kxmall.common.utils.poi.ExcelUtil;
import com.kxmall.common.core.page.TableDataInfo;

/**
 * 废品分类信息
 *
 * @author kxmall
 * @date 2023-11-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/wasteCategorize")
public class HsWasteCategorizeController extends BaseController {

    private final IHsWasteCategorizeService iHsWasteCategorizeService;

    /**
     * 查询废品分类信息列表
     */
    @SaCheckPermission("system:wasteCategorize:list")
    @GetMapping("/list")
    public TableDataInfo<HsWasteCategorizeVo> list(HsWasteCategorizeBo bo, PageQuery pageQuery) {
        if(bo == null){
            bo = new HsWasteCategorizeBo();
        }
        bo.setParentId(0l);

        TableDataInfo<HsWasteCategorizeVo> datas = iHsWasteCategorizeService.queryPageList(bo, pageQuery);
        for(HsWasteCategorizeVo hsWasteCategorizeVo :datas.getRows()){
            List<HsWasteCategorizeVo> chidList = iHsWasteCategorizeService.queryListByParentId(hsWasteCategorizeVo.getId());
            hsWasteCategorizeVo.setChildCategorizeVoList(chidList);
        }
        return datas;
    }

    /**
     * 根据上级获取废品分类列表信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:wasteCategorize:queryByParentId")
    @GetMapping("/listByParentId/{id}")
    public R<List<HsWasteCategorizeVo>> getListByParentId(@PathVariable Long id) {
        if(id == null){
            id = 0l;
        }
        return R.ok(iHsWasteCategorizeService.queryListByParentId(id));
    }

    /**
     * 导出废品分类信息列表
     */
    @SaCheckPermission("system:wasteCategorize:export")
    @Log(title = "废品分类信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HsWasteCategorizeBo bo, HttpServletResponse response) {
        List<HsWasteCategorizeVo> list = iHsWasteCategorizeService.queryList(bo);
        ExcelUtil.exportExcel(list, "废品分类信息", HsWasteCategorizeVo.class, response);
    }

    /**
     * 获取废品分类信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:wasteCategorize:query")
    @GetMapping("/{id}")
    public R<HsWasteCategorizeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iHsWasteCategorizeService.queryById(id));
    }

    /**
     * 新增废品分类信息
     */
    @SaCheckPermission("system:wasteCategorize:add")
    @Log(title = "废品分类信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HsWasteCategorizeBo bo) {
        return toAjax(iHsWasteCategorizeService.insertByBo(bo));
    }

    /**
     * 修改废品分类信息
     */
    @SaCheckPermission("system:wasteCategorize:edit")
    @Log(title = "废品分类信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HsWasteCategorizeBo bo) {
        return toAjax(iHsWasteCategorizeService.updateByBo(bo));
    }

    /**
     * 删除废品分类信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:wasteCategorize:remove")
    @Log(title = "废品分类信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iHsWasteCategorizeService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
